package sky.demo.server.si;

import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import sky.demo.client.UserService;
import sky.demo.client.dto.UserDto;
import sky.demo.persistence.dao.UserDao;
import sky.demo.persistence.entity.User;

/**
 * @author sky
 */

@Service
@Transactional(readOnly = true)
public class UserServiceImpl implements UserService {

    static DozerBeanMapper mapper = new DozerBeanMapper();

    @Autowired
    UserDao userDao;

    /**
     * @see sky.demo.client.UserService#getUser(java.lang.String)
     **/
    public UserDto getUser(Integer userId) {
        User findOne = userDao.findOne(userId);
        UserDto dto = mapper.map(findOne, UserDto.class);
        return dto;
    }

    /**
     * //TODO 添加override说明
     * @see sky.demo.client.UserService#addUser(sky.demo.client.dto.UserDto)
     **/
    @Override
    @Transactional
    public UserDto addUser(UserDto userDto) {
        User user = mapper.map(userDto, User.class);
        User save = userDao.save(user);
        return mapper.map(save, UserDto.class);
    }

}
